//
// Created by lingzhitech on 25-8-6.
//
#include "Sales_data.h"

Sales_data::Sales_data(std::istream &is) {
    is >> *this;
}

Sales_data &Sales_data::combine(const Sales_data &s) {
    if (bookNo == s.bookNo) {
        units_sold += s.units_sold;
        revenue += s.revenue;
    }
    return *this;
}

Sales_data &Sales_data::operator+=(const Sales_data &s) {
    return combine(s);
}


std::ostream &operator<<(std::ostream &os, const Sales_data &item) {
    os << item.isbn() << " " << item.units_sold << " "
        << item.revenue << " " << item.avg_price();
    return os;
}

std::istream &operator>>(std::istream &is, Sales_data &item) {
    double price;
    is >> item.bookNo >> item.units_sold >> price;
    if (is) {
        item.revenue = price * item.units_sold;
    } else {
        item = Sales_data();
    }
    return is;
}


Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs) {
    Sales_data result(lhs);
    result.combine(rhs);
    return result;
}

bool operator==(const Sales_data &lhs, const Sales_data &rhs) {
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
           lhs.isbn() == rhs.isbn();
}

bool operator!=(const Sales_data &lhs, const Sales_data &rhs) {
    return !(lhs == rhs);
}